package nio.zerocopy;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author: Dgg
 * @Date: 2021/12/6 21:36
 */
public class NewIoServer {

	public static void main(String[] args) throws Exception {
		InetSocketAddress inetSocketAddress = new InetSocketAddress(7001);

		ServerSocketChannel open = ServerSocketChannel.open();

		ServerSocket socket = open.socket();

		socket.bind(inetSocketAddress);

		ByteBuffer allocate = ByteBuffer.allocate(4096);

		while (true) {
			SocketChannel accept = open.accept();
			int readCount = 0;
			while (-1 != readCount) {
				try {
					readCount = accept.read(allocate);

				} catch (IOException e) {
					e.printStackTrace();
					break;
				}
				allocate.rewind();//倒带 positon = 0 mark作废
			}
		}
	}
}
